package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.FormContentService;
import cn.kys.modules.system.service.GetDeptIdByLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service
public class FormContentServiceImplzxw implements FormContentService {


    @Autowired
    private GetDeptIdByLoginUser getDeptIdByLoginUser;
    @Value("${adminUserIncludeStr}")
    private String adminUserIncludeStr;
    // 加部门权限的id
    public Long getDeptId() {
        return getDeptIdByLoginUser.getDeptId();
    }
    // 加部门权限的用户名
    public String getUserName() {
        return getDeptIdByLoginUser.getUserName();
    }



    @Autowired
    private ToolsResposity toolsResposity;


    @Override
    public Map addFormContent(String name, String cname,String type,String enumvalue) {
        Map returnMap = new HashMap();
//        String sql = "SELECT * FROM public.t_formcontent where name = '" + name + "'";
        String sql = String.format("SELECT * FROM public.t_formcontent where name ='%s' and dept_id=%d",name,getDeptId());
        Map formContentMap = toolsResposity.exeSql(sql);
        if(formContentMap==null){

//            String sql_insert = "INSERT INTO public.t_formcontent(name,cname,type,enum,dept_id) VALUES ('" + name + "','" + cname +"','"+type+ "','"+enumvalue+"');";
            String sql_insert = String.format("INSERT INTO public.t_formcontent(name,cname,type,enum,dept_id) VALUES ('%s','%s','%s','%s',%d)",name,cname,type,enumvalue,getDeptId());
            toolsResposity.exeSql(sql_insert);
            returnMap.put("success",true);

        }else {
            returnMap.put("success",false);
            returnMap.put("msg","表单项目已经存在");
        }

        return returnMap;
    }


    @Override
    public void addMultiFormContents(String jsonStr) {

    }



    @Override
    public Map editFormContent(String id, String name, String cname, String type,String enumvalue) {
        Map resultMap = new HashMap();

        List<Map> nameList = toolsResposity.queryNameFromFormContent();
        for (Map map : nameList) {
            //判断更新的是不是同一条，是的话
            String idStr = map.get("id").toString();
            String nameStr = map.get("name").toString();
            if (!id.equals(idStr) && name.equals(nameStr)) {
                resultMap.put("success", false);
                resultMap.put("msg", name + "已经存在");
                return resultMap;
            }
        }

        String sql = "UPDATE t_formcontent SET cname = '" + cname + "',name='" + name + "',type='" + type +"',enum='"+enumvalue+"' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);
        resultMap.put("success",true);
        resultMap.put("msg", "编辑成功");
        return resultMap;
    }

    @Override
    public List getAllFormcontents() {

        String sql = "";
        if (getUserName().contains(adminUserIncludeStr)) {
            sql = String.format("SELECT * FROM public.t_formcontent");
        } else {
            sql = String.format("SELECT * FROM public.t_formcontent where dept_id=%d", getDeptId());
        }
        return toolsResposity.exeSqlList(sql);

    }

    @Override
    public List getFormContentById(String fieldId) {
        return null;
    }

    @Override
    public Map importFormContentsByExcel(MultipartFile file, int layerid) {
        return null;
    }

    @Override
    public Map delFormContentById(String id) {
        Map returnMap = new HashMap();

        String sql = "select * from t_form_content where formcontentid in (" + id + ")";
        List formContentMap = toolsResposity.exeSqlList(sql);
        if (formContentMap.size() > 0) {
            //不能删
            returnMap.put("deleted", false);
            returnMap.put("msg", "表单表中使用了该图层，不能删除");
        } else {
            //可以删除
            String sql_delete = "delete from t_formcontent where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);
            returnMap.put("deleted", true);
        }

        return returnMap;
    }

    @Override
    public List getAllContentTypes() {
        String sql = "SELECT * FROM public.t_contenttype";
        return toolsResposity.exeSqlList(sql);
    }


}

